"""
防空导弹发射器
"""

import Entities.Missile_FIM as fim
import numpy as np
import json,math,os

class SAM():
    def __init__(self,name,lable,a=0,p=0.):
        para = json.load(open('Config/EntitiesConfig.json', 'r', encoding='UTF-8'))
        self.Name = name
        self.Lable = lable
        self.Speed=para['SAM_Speed']
        self.Accelerate=a
        self.Position=[]
        self.Theta=math.pi/2   # 俯仰,初始水平
        self.Phi=p  # 偏转
        self.Pointing=[self.Theta,self.Phi]
        self.life=1 # 生存标记
        self.N_M=para['UAV_N_Missiles'] # 剩余弹药数量
        self.Missile_list=[fim.FIM(str(i),self.Name,self.Lable) for i in range(self.N_M)]  # 剩余载弹
        self.P_destroyed=0. # 被摧毁概率 是个过程量

    def Movie_angle(self,p,dt):
        '''
        按角度飞
        :param p: 偏转Phi
        :param dt: 时间片长度，单位s
        :return: 无
        '''
        assert p >= -math.pi / 18 and p <= math.pi / 18, '输入角度Phi超出范围' + str(p)
        self.Phi+=p
        dx = self.Speed * math.sin(self.Theta) * math.cos(self.Phi) * dt
        dy = self.Speed * math.sin(self.Theta) * math.sin(self.Phi) * dt
        dz = self.Speed * math.cos(self.Theta) * dt
        # 判断该空中单位是否已被击毁，若被击毁则不进行坐标更新，若未被击毁则进行坐标更新
        if self.life == 1:
            self.Position[0] += dx
            self.Position[1] += dy
            self.Position[2] += dz

    def Movie_grid(self,offset_x,offset_y):
        '''
        网格飞行
        :param offset_x: x偏移量
        :param offset_y: y偏移量
        :return: 无
        '''
        self.Position[0]+=offset_x
        self.Position[1]+=offset_y

    def Movie_model(self,N_model):
        '''
        按动作库飞
        :param N_model: 第N号动作
        :return: 无
        '''
        if N_model==1:
            pass

    def Attack(self,target,pos_opponent,round):
        if len(self.Missile_list) > 0:
            missile=self.Missile_list.pop()
            missile.Launch(target,self.Position,pos_opponent,round)
            return missile
        else: return None